﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Portal.Core.DAL;
using Portal.BO.Editoral.LogNews;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsThread
{
    public partial class ThreadList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void grdThreadList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdThreadList.EditIndex = e.NewEditIndex;
        }

        protected void grdThreadList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.grdThreadList.EditIndex = -1;
        }

        protected void grdThreadList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow grdrow = this.grdThreadList.Rows[e.RowIndex];
            if (grdrow != null)
            {
                // Thực hiện cập nhật lại gridview
                TextBox txtTitle = grdrow.FindControl("txtEditThreadTitle") as TextBox;
                CheckBox chkEditThreadIsFocus = grdrow.FindControl("chkEditThreadIsFocus") as CheckBox;
                FileUpload flLogo = grdrow.FindControl("flELogo") as FileUpload;
                ImageButton btnSave=grdrow.FindControl("imgSave") as ImageButton;
                if(btnSave !=null)
                {
                    objThreadlistSource.UpdateParameters[0].DefaultValue = btnSave.CommandArgument;
                    objThreadlistSource.UpdateParameters[1].DefaultValue = txtTitle.Text;
                    objThreadlistSource.UpdateParameters[2].DefaultValue = chkEditThreadIsFocus.Checked.ToString();
                    objThreadlistSource.UpdateParameters[3].DefaultValue = flLogo.FileName;
                    objThreadlistSource.Update();

                    //Log sự kiện vào database
                    LogRow logRow = new LogRow();
                    logRow.Object_ID = 0; logRow.News_ID = 0;
                    logRow.Type = (int)LogType.FlowEvent;
                    logRow.UserName = HttpContext.Current.User.Identity.Name;
                    logRow.CreatedDate = DateTime.Now;
                    logRow.Action = LogListName.FLOW_EVENT + String.Format(Log.CAPNHAT_FLOWEVENT,txtTitle.Text);
                    LogHelper logHelper = new LogHelper();
                    logHelper.InsertFlowEvent(logRow);
                }
            }
            //this.grdListNewsThread.AutoGenerateEditButton = false;
            this.grdThreadList.EditIndex = -1;
        }

        protected void grdThreadList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow grdrow = this.grdThreadList.Rows[e.RowIndex];
            if (grdrow != null)
            {
                ImageButton btnSave = grdrow.FindControl("imgSave") as ImageButton;
                TextBox txtTitle = grdrow.FindControl("txtEditThreadTitle") as TextBox;

                if (btnSave != null)
                {
                    objThreadlistSource.DeleteParameters[0].DefaultValue = btnSave.CommandArgument;
                    int countDeleted = objThreadlistSource.Delete();
                    if (countDeleted <= 0)
                    {
                        Page.RegisterClientScriptBlock("abcd","<script language='javascript'>alert('Không thể xóa luồng sự kiện vì có tin liên quan')</script>");
                    }
                    #region Log Bo Di
                    //Log sự kiện vào database
                    /*
                    LogRow logRow = new LogRow();
                    logRow.Object_ID = 0; logRow.News_ID = 0;
                    logRow.Type = (int)LogType.FlowEvent;
                    logRow.UserName = HttpContext.Current.User.Identity.Name;
                    logRow.CreatedDate = DateTime.Now;
                    logRow.Action = LogListName.FLOW_EVENT + String.Format(Log.XOA_FLOAEVENT, txtTitle.Text);
                    LogHelper logHelper = new LogHelper();
                    logHelper.InsertFlowEvent(logRow);
                     */
                    #endregion
                }
            }
            this.grdThreadList.EditIndex = -1;
        }

        protected void grdThreadList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "NewThread".ToLower())
            {
                GridViewRow grdrow = this.grdThreadList.FooterRow;
                if (grdrow != null)
                {
                    TextBox txtTitle = grdrow.FindControl("txtThreadTitle") as TextBox;
                    CheckBox chkThreadIsFocus = grdrow.FindControl("chkIsFocus") as CheckBox;
                    FileUpload flLogo = grdrow.FindControl("flLogo") as FileUpload;
                    if (txtTitle != null)
                    {
                        if (txtTitle.Text != "")
                        {
                            objThreadlistSource.InsertParameters[0].DefaultValue = txtTitle.Text;
                            objThreadlistSource.InsertParameters[1].DefaultValue = chkThreadIsFocus.Checked.ToString();
                            objThreadlistSource.InsertParameters[2].DefaultValue = flLogo.FileName;
                            objThreadlistSource.Insert();

                            //Log sự kiện vào database
                            LogRow logRow = new LogRow();
                            logRow.Object_ID = 0; logRow.News_ID = 0;
                            logRow.Type = (int)LogType.FlowEvent;
                            logRow.UserName = HttpContext.Current.User.Identity.Name;
                            logRow.CreatedDate = DateTime.Now;
                            logRow.Action = LogListName.FLOW_EVENT + String.Format(Log.THEMMOI_FLOWEVENT, txtTitle.Text);
                            LogHelper logHelper = new LogHelper();
                            logHelper.InsertFlowEvent(logRow);
                        }
                    }
                }

                Response.Redirect("/office/threadsmgr.chn");
            }
        }

        protected void cboPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdThreadList.PageIndex = Convert.ToInt32(cboPage.SelectedValue);
        }

        protected void lnkRealDel_Click(object sender, EventArgs e)
        {
            String value = getCheckedRow();

            if (!value.Equals(String.Empty))
            {
                objThreadlistSource.DeleteParameters[0].DefaultValue = value;
                String[] Thread_ID = value.Split(',');
                int TotalThreadDelete = Thread_ID.Length;
                #region Log Bo di
                /*
                LogHelper logHelper = new LogHelper();
                LogRow logRow = new LogRow();
                for (int i = 0; i < TotalThreadDelete; i++)
                {
                    logRow.Object_ID = 0; logRow.News_ID = 0;
                    logRow.Type = (int)LogType.FlowEvent;
                    logRow.UserName = HttpContext.Current.User.Identity.Name;
                    logRow.CreatedDate = DateTime.Now;
                    logRow.Action = LogListName.FLOW_EVENT + String.Format(Log.XOA_FLOAEVENT, logHelper.GetTitleNewsThread(Convert.ToInt32(Thread_ID[i])));
                    logHelper.InsertFlowEvent(logRow);
                }*/
                #endregion
                int countDeleted = objThreadlistSource.Delete();
                //if (countDeleted <= 0)
                //{
                //    Page.RegisterClientScriptBlock("abcd", "<script language='javascript'>alert('Có một luồng sự kiện không xóa được vì có tin liên quan')</script>");
                //    return;
                //}
            }
            Response.Redirect("/office/threadsmgr.chn");
        }
        private string getCheckedRow()
        {
            string strDel = "";
            strDel = Request.Form["chkSelect"];

            #region bo di
            /*
            foreach (GridViewRow grdRow in this.grdThreadList.Rows)
            {
                if (grdRow.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkSel = (CheckBox)grdRow.Cells[0].FindControl("chkSelect");
                    if (chkSel.Checked)
                    {
                        ImageButton btnItem = (ImageButton)grdRow.Cells[5].FindControl("imgEdit");
                        if (strDel != "")
                            strDel += ",";
                        strDel += btnItem.CommandArgument;
                    }
                }
            }
             */
            #endregion
            //strDel += "(" + strDel + ")";
            if (strDel == null)
            {
                return String.Empty;
            }
            return strDel;
        }

        /*
         * dungtm 27/03/2008 chuyen phan them moi luong ra khoi gridview
         */
        protected void btnNewThread_Click(object sender, ImageClickEventArgs e)
        {
            
                if (txtThreadTitle.Text != "")
                {
                    objThreadlistSource.InsertParameters[0].DefaultValue = txtThreadTitle.Text;
                    objThreadlistSource.InsertParameters[1].DefaultValue = chkIsFocus.Checked.ToString();
                    objThreadlistSource.InsertParameters[2].DefaultValue = flLogo.FileName;
                    objThreadlistSource.Insert();

                    //Log sự kiện vào database
                    LogRow logRow = new LogRow();
                    logRow.Object_ID = 0; logRow.News_ID = 0;
                    logRow.Type = (int)LogType.FlowEvent;
                    logRow.UserName = HttpContext.Current.User.Identity.Name;
                    logRow.CreatedDate = DateTime.Now;
                    logRow.Action = LogListName.FLOW_EVENT + String.Format(Log.THEMMOI_FLOWEVENT, txtThreadTitle.Text);
                    LogHelper logHelper = new LogHelper();
                    logHelper.InsertFlowEvent(logRow);
                }
                
                Response.Redirect("/office/threadsmgr.chn");
            
        }
    }
}